<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]" :visible.sync="dialogVisible" width="50%" append-to-body>
      <el-form ref="form" :model="form" :disabled="flag==1">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="出库单号" prop="orderNo" required>
              <el-input v-model="form.orderNo" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出库类型" prop="optType" required>
              <el-select v-model="form.optType" style="width: 100%">
                <el-option
                  v-for="item in dict.type.wms_shipment_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="warehouseId" required>
              <el-select v-model="form.warehouseId" clearable filterable style="width: 100%"
                         @change="changeStore">
                <el-option
                  v-for="val in storeList"
                  :key="val.id"
                  :label="val.warehouseName"
                  :value="val.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓位名称" prop="warehouseSpaceId" required>
              <el-select v-model="form.warehouseSpaceId" clearable filterable style="width: 100%">
                <el-option
                  v-for="val in spaceList"
                  :key="val.id"
                  :label="val.spaceName"
                  :value="val.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="总数量" prop="totalQuantity" required :rules="[
            { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入最多2位小数的数字', trigger: 'blur' }
                          ]">
              <el-input v-model="form.totalQuantity" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总金额" prop="totalAmount" required :rules="[
            { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入最多2位小数的数字', trigger: 'blur' }
                          ]">
              <el-input v-model="form.totalAmount" placeholder="请输入" maxlength="50"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="供应商" prop="merchantId" required>
              <el-select v-model="form.merchantId" clearable filterable style="width: 100%">
                <el-option
                  v-for="val in unitList"
                  :key="val.id"
                  :label="val.merchantName"
                  :value="val.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="form.remark" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <titleDiv name="产品信息"></titleDiv>
      <div>
        <el-button type="primary" plain @click="$refs.selectGoodDialog.show(1)">新增</el-button>
        <el-button @click="list=[]">清空</el-button>
      </div>
      <el-table
        :data="list"
        v-loading="loading"
        max-height="500"
      >
        <el-table-column type="index" label="序号" align="center"></el-table-column>
        <el-table-column prop="productName" label="产品信息" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.product.productName }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="skuName" label="规格信息" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.productSku.skuName }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="quantity" label="数量" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.quantity }}</div>
            <el-input-number v-else v-model="scope.row.quantity" min="0" placeholder="请输入"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column prop="amount" label="金额" align="center">
          <template slot-scope="scope">
            <div v-if="flag==1">{{ scope.row.amount }}</div>
            <el-input-number v-else v-model="scope.row.amount" min="0" placeholder="请输入"></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" v-if="flag!=1">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="deleteRow(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogSave" v-if="flag!=1">确 定</el-button>
        <el-button type="success" @click="dialogOk" v-if="flag!=1">完成出库</el-button>
      </div>
    </el-dialog>
    <selectGoodDialog ref="selectGoodDialog" @ok="selectGoodDialogOk"></selectGoodDialog>
  </div>
</template>

<script>
import {updateOutStore, listStore, listStoreSpace, listUnit, addOutStore, okAddOutStore} from "../../../api/imsMng";
import selectGoodDialog from "@/myComp/selectGoodDialog";

export default {
  dicts: ['wms_shipment_type'],
  components: {selectGoodDialog},
  data() {
    return {
      flag: 0,
      loading: false,
      dialogVisible: false,
      form: {},
      list: [],
      titleList: ['新增', '详情', '修改'],
      storeList: [],
      spaceList: [],
      unitList: []
    }
  },
  methods: {
    show(flag, form) {
      this.flag = flag
      this.dialogVisible = true
      this.resetForm('form')
      this.form = form
      this.load()
    },
    changeStore(e) {
      if (e) {
        this.$set(this.form, 'warehouseSpaceId', '')
      }
      listStoreSpace({id: this.form.warehouseId}).then(res => {
        this.spaceList = res.rows
      })
    },
    load() {
      listStore().then(res => {
        this.storeList = res.rows
      })
      listStoreSpace().then(res => {
        this.spaceList = res.rows
      })
      listUnit().then(res => {
        this.unitList = res.rows
      })
    },
    deleteRow(row) {
      this.list = this.list.filter(val => val != row)
    },
    selectGoodDialogOk(row) {
      this.list.push(row)
    },
    dialogSave() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.details = this.list
          if (this.flag == 2) {
            updateOutStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            })
          } else {
            addOutStore(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            })
          }
        }
      })
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.details = this.list
          okAddOutStore(this.form).then(res => {
            this.$emit('ok')
            this.dialogVisible = false
            this.$message.success('出库成功')
          })
        }
      })
    },
  }
}
</script>
